package com.example.safeassistan.home.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.adapter.BackLogAdapter;
import com.example.safeassistan.home.adapter.RiskDetailRiskFragmentPAdapter;
import com.example.safeassistan.home.model.BackLogFragmentModel;
import com.example.safeassistan.home.model.MyBackLogListModel;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.risk.model.RiskPointDetailRisk;
import com.example.safeassistan.utile.JsonUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ActivityBacklogList extends BaseActivity {

    private BackLogAdapter adapter;
    private RecyclerView rcView;
    private LinearLayout llNodata;
    private SmartRefreshLayout refresh;
    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ActivityBacklogList.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.backlog_list;
    }

    @Override
    protected void initView() {
        setTitleText("待办任务列表");
        rcView=findViewById(R.id.rcView);
        refresh=findViewById(R.id.refresh);
        llNodata=findViewById(R.id.llNodata);
        setView();
    }

    public  void  setView(){
        refresh.setEnableRefresh(true);
        refresh.setEnableLoadMore(false);
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getData();
            }
        });
    }

    @Override
    protected void initData() {
        getData();
    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }

    public void getData() {
        new HttpRequest(ActivityBacklogList.this).doGet(UrlConstants.getMyTaskList, null, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    int code = jsonObject.getInt("code");
                    if (code == 200) {
                        refresh.finishRefresh();
                        refresh.finishLoadMore();
                        Object obj = JsonUtils.fromJson(result.toString(), MyBackLogListModel.class);
                        if (obj instanceof MyBackLogListModel) {
                            MyBackLogListModel backLogListModel = (MyBackLogListModel) obj;
                            if (backLogListModel.getResult().size() > 0) {
                                llNodata.setVisibility(View.GONE);
                                rcView.setLayoutManager(new LinearLayoutManager(ActivityBacklogList.this));
                                adapter = new BackLogAdapter(backLogListModel.getResult());
                                rcView.setAdapter(adapter);
                                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        if (backLogListModel.getResult().get(position).getType() == 1) {
                                            //分级管控
                                            ActivityBacksecond.actionStart(getContext(), backLogListModel.getResult().get(position).getId(),
                                                    backLogListModel.getResult().get(position).getCheckTableId());
                                        } else {
                                            List<BackLogFragmentModel.ResultDTO.RecordsDTO.DangerVOListDTO> list=new ArrayList<>();
                                            BackLogFragmentModel.ResultDTO.RecordsDTO.DangerVOListDTO m=new BackLogFragmentModel.ResultDTO.RecordsDTO.DangerVOListDTO();
                                            m.setSelect(true);
                                            m.setId("");
                                            m.setDangerSrcName("");
                                            list.add(m);
                                            HiddenDangerinvestigation.actionStart(2,ActivityBacklogList.this,
                                                    backLogListModel.getResult().get(position).getId(),
                                                    backLogListModel.getResult().get(position).getRiskPointName()==null?"":backLogListModel.getResult().get(position).getRiskPointName(), 1,
                                                    backLogListModel.getResult().get(position).getRiskPointId(), backLogListModel.getResult().get(position).getCheckTableId(),
                                                    list);
                                        }
                                    }
                                });
                            } else {
                                adapter.notifyDataSetChanged();
                                llNodata.setVisibility(View.VISIBLE);
                            }
                        }
                    } else {
                        Toast.makeText(ActivityBacklogList.this, message, Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                refresh.finishRefresh();
                refresh.finishLoadMore();
            }
        });
    }


}